// 猜数字游戏

use rand::Rng;
use std::{cmp::Ordering, io};

fn main() {
    println!("gussing number!");
    let secret_num = rand::thread_rng().gen_range(1..=100);

    loop {
        println!("please enter your guess number: ");
        let mut guess: String = String::new();
        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to readline");

        // let guess: u32 = guess.trim().parse().expect("please input a number");
        
        // 处理无效输入: 非数字时重新输入
        let guess:u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue
        };

        println!("You guessed: {}", guess);
        match guess.cmp(&secret_num) {
            Ordering::Greater => println!("bigger"),
            Ordering::Less => println!("smaller"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}
